package com.hutu.tree;

import com.hutu.util.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

public class MinDepth {

    /**
     * 二叉树的最小深度
     */
    static int minDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        Queue<TreeNode> q = new LinkedList<>();
        q.offer(root);
        // root 本身就是一层，depth 初始化为 1
        int depth = 1;

        while (!q.isEmpty()) {
            int sz = q.size();
            /* 将当前队列中的所有节点向四周扩散 */
            for (int i = 0; i < sz; i++) {
                TreeNode cur = q.poll();
                /* 判断是否到达终点 */
                if (cur.left == null && cur.right == null) {
                    return depth;
                }
                /* 将 cur 的相邻节点加入队列 */
                if (cur.left != null) {
                    q.offer(cur.left);
                }
                if (cur.right != null) {
                    q.offer(cur.right);
                }
            }
            /* 这里增加步数 */
            depth++;
        }
        return depth;
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(0,
                new TreeNode(11,
                        new TreeNode(21,
                                new TreeNode(31, null, null),
                                new TreeNode(32, null, null)),
                        new TreeNode(22,
                                new TreeNode(33, null, null),
                                new TreeNode(34, null, null)
                        )
                ),
                new TreeNode(12,
                        new TreeNode(23, null, null),
                        new TreeNode(24, null, null)
                )
        );
        System.out.println(minDepth(root));
    }
}
